﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DocumentTrackingSystem.Models;
using MvcPaging;
namespace DocumentTrackingSystem.Service
{
    public class OfficeServices : Repository<Office>, IOfficeServices
    {
        public int FindAndUpdateOffice(Office viewModel)
        {
            var _office = this.Find(x => x.ID == viewModel.ID);
            if (_office !=null)
            {
                _office.Name = viewModel.Name;
                _office.ShortName = viewModel.ShortName;
                return this.Update(_office);
               
            }
            return 0;
        }
        public int InsertOffice(Office viewModel, out Office office)
        {
            Office _office = new Office();
            _office.Name = viewModel.Name;
            _office.ShortName = viewModel.ShortName;
            office = this.Create(_office);
            return 1;
        }
        public IPagedList<Office> CustomerListWithPagingAndSorting(int currentPage, string sortBy, int PageSize)
        {
            IQueryable<Office> query  = this.All();
            switch (sortBy)
            {
                case "name":
                    query = query.OrderBy(x => x.Name);
                    break;

                case "shortname":
                    query = query.OrderBy(x => x.ShortName);
                    break;


                default:
                    query = query.OrderBy(x => x.ID);
                    break;
            }
            IPagedList<Office>  _officelist = query.ToPagedList(currentPage,PageSize);
            return _officelist;
        }
    }
}